<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="场地名称" prop="locationName">
            <el-input v-model="formData.locationName" placeholder="请输入场地名称" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="建筑物ID" prop="buildingId">
            <el-input-number v-model="formData.buildingId" placeholder="请输入建筑物ID" style="width: 100%" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="楼层" prop="floor">
            <el-input-number v-model="formData.floor" placeholder="请输入楼层" style="width: 100%" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="房间号" prop="roomNumber">
            <el-input v-model="formData.roomNumber" placeholder="请输入房间号" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="场地类型" prop="locationType">
            <el-select v-model="formData.locationType" placeholder="请选择场地类型" style="width: 100%">
              <el-option label="机房" value="机房" />
              <el-option label="实训室" value="实训室" />
              <el-option label="教室" value="教室" />
              <el-option label="会议室" value="会议室" />
              <el-option label="实验室" value="实验室" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="教室人数" prop="classroomCapacity">
            <el-input-number
              v-model="formData.classroomCapacity"
              :min="1"
              :max="999"
              placeholder="请输入教室人数"
              style="width: 100%"
            />
          </el-form-item>
        </el-col>
      </el-row>qiqi

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="管理员ID" prop="managerId">
            <el-input-number v-model="formData.managerId" placeholder="请输入管理员ID" style="width: 100%" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="状态" prop="status">
            <el-select v-model="formData.status" placeholder="请选择状态" style="width: 100%">
              <el-option label="禁用" :value="0" />
              <el-option label="启用" :value="1" />
              <el-option label="维修中" :value="2" />
              <el-option label="预约中" :value="3" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>

      <el-form-item label="场地详细信息" prop="locationDetailInfo">
        <el-input
          v-model="formData.locationDetailInfo"
          type="textarea"
          :rows="3"
          placeholder="请输入场地详细信息，如位置、设备配置等"
        />
      </el-form-item>

      <el-form-item label="设备清单" prop="equipmentList">
        <el-input
          v-model="formData.equipmentList"
          type="textarea"
          :rows="3"
          placeholder="请输入设备清单，如投影仪、电脑、音响设备等"
        />
      </el-form-item>

      <el-form-item label="预约规则" prop="bookingRules">
        <el-input
          v-model="formData.bookingRules"
          type="textarea"
          :rows="3"
          placeholder="请输入预约规则，如预约时间、使用限制等"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { ClassLocationApi, ClassLocation } from '@/api/ao/classlocation'

/** 上课场地信息 表单 */
defineOptions({ name: 'ClassLocationForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  locationId: undefined,
  locationName: undefined,
  buildingId: undefined,
  floor: undefined,
  roomNumber: undefined,
  locationType: undefined,
  locationDetailInfo: undefined,
  classroomCapacity: undefined,
  equipmentList: undefined,
  managerId: undefined,
  bookingRules: undefined,
  status: 1, // 默认启用状态
})
const formRules = reactive({
  locationName: [{ required: true, message: '场地名称不能为空', trigger: 'blur' }],
  status: [{ required: true, message: '状态不能为空', trigger: 'change' }],
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await ClassLocationApi.getClassLocation(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as ClassLocation
    if (formType.value === 'create') {
      await ClassLocationApi.createClassLocation(data)
      message.success(t('common.createSuccess'))
    } else {
      await ClassLocationApi.updateClassLocation(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    locationId: undefined,
    locationName: undefined,
    buildingId: undefined,
    floor: undefined,
    roomNumber: undefined,
    locationType: undefined,
    locationDetailInfo: undefined,
    classroomCapacity: undefined,
    equipmentList: undefined,
    managerId: undefined,
    bookingRules: undefined,
    status: 1, // 默认启用状态
  }
  formRef.value?.resetFields()
}
</script>
